
//调用自己封装的axios 
import { putanswer,articlereply,getarticlereply,getallarticlereply,getcommentlist,setcomment,attention} from "@/service/main"
import LocalCache from "@/utils/cache"

import { ElMessage } from 'element-plus'


const comment = {
  namespaced: true,
  state() {
    return {
        commentdata:[],
        showWangeditor:true,
        answerlist:[],
        attention:[],
        fans:0
    }
  },
  getters:{
    getanswerlist:(state)=>{
      return state.answerlist
    },
    getcommentdata:(state)=>{
      console.log(state.commentdata,'state里的commentdata');
      return state.commentdata
    },
  },
  mutations: {
   changecommentdata(state,data) {
    state.commentdata = data
   },
   changeWangeditorstate(state){
    state.showWangeditor = !state.showWangeditor
   },
   changeanswerlist(state,data){
    state.answerlist = data
   },
   attention(state,data) {
    state.attention = data
   },
   fans(state,fans){
    console.log(fans,'fans');
    state.fans = fans
   }
  },
  actions: {
    //获取 评论  
    async getcomment({ commit }, payload){
        const commentlist = await getcommentlist(payload)
        console.log( commit,payload,'commentlist',commentlist);
        commit("changecommentdata",commentlist)
    },
    //设置评论 
    async setcomment({ commit }, payload){
      console.log(commit);
      await setcomment(payload)
    },
    //写文章/或者问题
    async putarticle({commit}, payload){
      console.log(commit,{...payload});
      const id = LocalCache.getCache('id')
        const isok = await putanswer({...payload,id})
        console.log(isok);
        if(isok) {
          ElMessage.success({
            message:'发布成功'
          })
        }else {
          ElMessage.success({
            message:'发布失败',
            type:"error"
          })
        }
    },
    //回答
    async putanswer({commit}, payload) {
      console.log("写回答",{commit}, {...payload});
      const id = LocalCache.getCache('id')
      const isok =  await articlereply({...payload, id})
      if(isok) {
        ElMessage.success({
          message:'回答成功'
        })
        commit("changeWangeditorstate")
      }else {
        ElMessage.success({
          message:'回答失败',
          type:"error"
        })
      }
    },
    //获取当前文章的回答
    async getarticlereply({commit}, payload){
      const id = LocalCache.getCache('id')
      const data = await getarticlereply({...payload,id})
      console.log(data,'gethuida',commit);
      commit("changeanswerlist",data[0])
      commit("attention",data[1])
      commit('fans',data[2][0].idcount)
    } ,
    //当点击了查看跟多按钮 
    async getallarticlereply({commit}, payload){
      const data = await getallarticlereply(payload)
      console.log(data,'点击了查看去不回答的',payload);
      commit("changeanswerlist",data)
      if (data.length <=0) {
        ElMessage({
          message:"别几把点了，没有多的评论，自己评论一条看看",
          type:"error"
        })
      }
    },
    //关注作者
    async attention ({commit},userid) {
        console.log(commit,userid);
        const res = await attention(userid)
        console.log(res,'res');
        commit('fans',res.ndata[0].idcount)
        if (res.type) {
          ElMessage.success(
            {
              message:res.msg,
            }
          )
        }else{
          ElMessage(
            {
              message:res.msg,
              type:"error",
            }
          )
        }
    }
  },
  modules: {}
}
export default comment
